package com.java110.accessControl.manufactor.adapt.accessControl.wiegand;

import com.java110.core.utils.BytesUtil;
import com.java110.core.utils.StringUtil;

/**
 * 微耕工具类
 */
public class WiegandUtil {
    /**
     * 创建发送数据
     *
     * @param cmd
     * @param machineCode
     * @param data
     * @return
     */
    public static String createSendData(String cmd, String machineCode, String data) {

        String sendData = "";
        sendData += "17";// type
        sendData += cmd;// 功能号
        sendData += "0000";// 保留
        long machineCodeInt = Long.parseLong(machineCode); // 要转换的十进制数
        String machineCodeHex = Long.toHexString(machineCodeInt);
        sendData += machineCodeHex;// 设备序列号
        sendData += data;

        return padRight(sendData, 64 * 2, '0');
    }

    public static WiegandDataDto parseData(String data) {
        int typePos = 0; // 类型
        int typeLen = 1;
        int cmdPos = typePos + typeLen * 2; // 功能号
        int cmdLen = 1;
        int reservedPos = cmdPos + cmdLen * 2; // 保留
        int reservedLen = 2;
        int machineCodePos = reservedPos + reservedLen * 2; // 设备编号
        int machineCodeLen = 4;
        int dataPos = machineCodePos + machineCodeLen * 2;
        int dataLen = 32;
        int sequenceIdPos = dataPos + dataLen * 2;
        int sequenceIdLen = 4;

        String type = getTargetString(data, typePos, typeLen * 2);
        String cmd = getTargetString(data, cmdPos, cmdLen * 2);
        String machineCode = getTargetIntString(data, machineCodePos, machineCodeLen * 2);
        String businessData = getTargetString(data, dataPos, dataLen * 2);
        String sequenceId = getTargetString(data, sequenceIdPos, sequenceIdLen * 2);
        WiegandDataDto wiegandDataDto = new WiegandDataDto();
        wiegandDataDto.setCmd(cmd);
        wiegandDataDto.setMachineCode(machineCode);
        wiegandDataDto.setType(type);
        wiegandDataDto.setSequenceId(sequenceId);
        wiegandDataDto.setBusinessData(businessData);
        wiegandDataDto.setOrgData(data);
        return wiegandDataDto;

    }



    public static WiegandDataDto parseOpenDoorResult(String data) {
        int typePos = 0; // 类型
        int typeLen = 1;
        int cmdPos = typePos + typeLen * 2; // 功能号
        int cmdLen = 1;
        int reservedPos = cmdPos + cmdLen * 2; // 保留
        int reservedLen = 2;
        int machineCodePos = reservedPos + reservedLen * 2; // 设备编号
        int machineCodeLen = 4;
        int wiegandNumPos = machineCodePos + machineCodeLen * 2; // 索引号
        int wiegandNum = 4;
        int upTypePos = wiegandNumPos + wiegandNum * 2; // 记录类型
        int upTypeLen =1;
        int statePos = upTypePos + upTypeLen*2; // 有效性
        int stateLen = 1;
        int gateNumPos = statePos + stateLen *2;
        int gateNumLen = 1;
        int inoutPos = gateNumPos + gateNumLen*2;
        int inoutLen = 1;
        int cardPos = inoutPos + inoutLen*2;
        int cardLen = 4;

        String type = getTargetString(data, typePos, typeLen * 2);
        String cmd = getTargetString(data, cmdPos, cmdLen * 2);
        String machineCode = getTargetIntString(data, machineCodePos, machineCodeLen * 2);
        String gateNum = getTargetString(data, gateNumPos, gateNumLen * 2);
        String inout = getTargetString(data, inoutPos, inoutLen * 2);
        String cardNum = getTargetString(data, cardPos, cardLen * 2);
        String state = getTargetString(data, statePos, stateLen * 2);
        String sequenceId = getTargetString(data, wiegandNumPos, wiegandNum * 2);
        WiegandDataDto wiegandDataDto = new WiegandDataDto();
        wiegandDataDto.setCmd(cmd);
        wiegandDataDto.setMachineCode(machineCode);
        wiegandDataDto.setType(type);
        wiegandDataDto.setGateNum(gateNum);
        wiegandDataDto.setCardNum(cardNum);
        wiegandDataDto.setInout(inout);
        wiegandDataDto.setOrgData(data);
        wiegandDataDto.setState(state);
        wiegandDataDto.setSequenceId(sequenceId);
        return wiegandDataDto;
    }

    public static String getTargetString(String dataStr, int startPos, int dataLen) {
        String targetValue = "";
        if (StringUtil.isEmpty(dataStr)) {
            return targetValue;
        }
        if (dataStr.length() < startPos + dataLen) {
            return targetValue;
        }
        targetValue = dataStr.substring(startPos, startPos + dataLen);
        return targetValue;

    }

    public static String getTargetIntString(String dataStr, int startPos, int dataLen) {
        String targetValue = "";
        if (StringUtil.isEmpty(dataStr)) {
            return targetValue;
        }
        if (dataStr.length() < startPos + dataLen) {
            return targetValue;
        }
        targetValue = dataStr.substring(startPos, startPos + dataLen);
        return Long.parseLong(targetValue, 16) + "";

    }

    public static String padRight(String input, int length, char padChar) {
        if (input.length() >= length) {
            return input; // 如果字符串已经足够长，直接返回
        }

        StringBuilder sb = new StringBuilder(input);
        while (sb.length() < length) {
            sb.append(padChar); // 补充字符
        }
        return sb.toString();
    }

    // 方法：截取手机号后六位
    public static String getLastSixDigits(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.length() < 6) {
            return "000000";
        }
        return phoneNumber.substring(phoneNumber.length() - 6);
    }

    public static long hexLittleEndianToDecimal(String hex) {
        // 确保 hex 长度为 8（4字节）
        if (hex.length() != 8) {
            throw new IllegalArgumentException("Hex string must be 8 characters long (4 bytes)");
        }

        // 反转字节顺序（小端序 → 大端序）
        String reversedHex = reverseHexBytes(hex);

        // 解析为十进制
        return Long.parseLong(reversedHex, 16);
    }

    private static String reverseHexBytes(String hex) {
        StringBuilder reversed = new StringBuilder();
        for (int i = hex.length() - 2; i >= 0; i -= 2) {
            reversed.append(hex.substring(i, i + 2));
        }
        return reversed.toString();
    }

}
